package com.phone580.app.apphome;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;

import com.phone580.app.R;
import com.phone580.app.untilty.CommonUtility;

public class AppHomeActivity extends TabActivity {
	
	private TabHost tabHost;
	private View tabTitleView1, tabTitleView2;
	public static AppHomeActivity instance;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		instance = this;
		setContentView(R.layout.app_home_layout);
		initHeader("软件搬家");
		initTitleView();
		updateTitleDisplaySize();
		
		tabHost = getTabHost();
		TabSpec tabSpec1 = tabHost.newTabSpec("phone");
		tabSpec1.setIndicator(tabTitleView1);
		tabSpec1.setContent(new Intent(this, AppOnPhoneActivity.class));
		tabHost.addTab(tabSpec1);
		
		TabSpec tabSpec2 = tabHost.newTabSpec("sdcard");
		tabSpec2.setIndicator(tabTitleView2);
		tabSpec2.setContent(new Intent(this, AppOnSDCardActivity.class));
		tabHost.addTab(tabSpec2);
	}
	
	private void initTitleView() {
		LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		tabTitleView1 = inflater.inflate(R.layout.app_home_tab_item, null);
		TextView phone = (TextView) tabTitleView1.findViewById(R.id.app_home_tab_title_tv);
		phone.setText(" 手机上可移动");
		phone.setCompoundDrawablesWithIntrinsicBounds(R.drawable.phone, 0, 0, 0);
		
		tabTitleView2 = inflater.inflate(R.layout.app_home_tab_item, null);
		TextView sdcard = (TextView) tabTitleView2.findViewById(R.id.app_home_tab_title_tv);
		sdcard.setText(" SD卡上可移动");
		sdcard.setCompoundDrawablesWithIntrinsicBounds(R.drawable.sdcard, 0, 0, 0);
	}
	
	/**
	 * 更新tab标题手机容量大小
	 */
	public void updateTitleDisplaySize() {
		TextView phoneAllSize = (TextView) tabTitleView1.findViewById(R.id.app_home_tab_full_tv);
		TextView phoneAvailSize = (TextView) tabTitleView1.findViewById(R.id.app_home_tab_use_tv);
		phoneAllSize.setText("可用：" + CommonUtility.getTotalRomSize(this));
		phoneAvailSize.setText("已用：" + CommonUtility.getAvailRom(this));
		
		TextView sdCardAllSize = (TextView) tabTitleView1.findViewById(R.id.app_home_tab_full_tv);
		TextView sdCardAvailSIze = (TextView) tabTitleView1.findViewById(R.id.app_home_tab_use_tv);
		sdCardAllSize.setText("可用：" + CommonUtility.getTotalSDCardSize(this));
		sdCardAvailSIze.setText("已用：" + CommonUtility.getAvailSDCard(this));
	}
	
	private void initHeader (String title) {
		final ImageView ivBack = (ImageView) findViewById(R.id.header_back_iv);
		ivBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		final TextView tvTitle = (TextView) findViewById(R.id.header_title_tv);
		tvTitle.setText(title);
	}

}
